package com.huawei.struts.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.huawei.struts.form.CatalogForm;
import com.huawei.struts.service.CatalogService;
import com.st.model.CatalogT;
import com.st.model.LjwCatalogT;

public class CatalogAction extends BaseAction {
	
	private static Logger logger=Logger.getLogger(CatalogAction.class);
	private final String showCatalog="/catalog.do?method=toShowCatalog&p_id=0";
	private final String addCatalog="/catalog.do?method=toAddCatalog";
	CatalogService catalogService;

	 
	public void setCatalogService(CatalogService catalogService) {
		this.catalogService = catalogService;
	}


	/**
	 * 显示菜单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward showCatalog(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CatalogForm from=(CatalogForm)form;
		logger.info("into showCatalog method ...");
		List list=catalogService.getALlCataLog();
		JSONArray jsonArray = JSONArray.fromObject(list);
		this.responseResult(request, response, jsonArray.toString());
		return null;
	}
	
	/**
	 * 跳转到显示菜单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toShowCatalog(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CatalogForm from=(CatalogForm)form;
		Integer p_id=new Integer(request.getParameter("p_id"));
		List list=catalogService.getALlCataLogByParentId(p_id);
		request.setAttribute("catalogList", list);
		request.setAttribute("p_id", p_id);
		return mapping.findForward("toShowCatalog");
	}
	
	/**
	 * 响应客户端
	 * @param request
	 * @param response
	 * @param result
	 * @throws IOException
	 */
	public void responseResult(HttpServletRequest request, HttpServletResponse response,String result) throws IOException
	{
		response.setContentType("text/html;charset=utf-8");           
		PrintWriter out=response.getWriter();   
		request.setCharacterEncoding("utf-8");
	    out.print(result);
	    out.close();
	}
	
	/**
	 * 跳转到添加菜单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toAddCatalog(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CatalogForm from=(CatalogForm)form;
		from.getCatalog().setParentId(from.getCatalog().getParentId());
		logger.info("into toAddCatalog method ...");
		return mapping.findForward("toAddCatalog");
	}
	/**
	 * 增加菜单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addCatalog(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CatalogForm from=(CatalogForm)form;
		logger.info("into addCatalog method ...");
		
		catalogService.saveCatalog(from.getCatalog());
		logger.info("save catalog success ...");
		return super.goSuccess("添加成功", "/catalog.do?method=toShowCatalog&p_id="+from.getCatalog().getParentId(), request, response, mapping);
	}
	
	/**
	 * 删除菜单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delCatalog(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CatalogForm from=(CatalogForm)form;
		Integer id=Integer.parseInt(request.getParameter("c_id"));
		from.getCatalog().setCatalogId(id);
		catalogService.deleteCatalog(from.getCatalog());
		return super.goSuccess("删除成功", "/catalog.do?method=toShowCatalog&p_id="+from.getCatalog().getParentId(), request, response, mapping);
	}
	
	/**
	 * 修改菜单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateCatalog(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CatalogForm from=(CatalogForm)form;
		logger.info("into updateCatalog method ...");
		catalogService.updateCatalog(from.getCatalog());
		logger.info("update catalog success ...");
		return super.goSuccess("修改成功", "/catalog.do?method=toShowCatalog&p_id="+from.getCatalog().getParentId(), request, response, mapping);
	}
	
	/**
	 * 跳转到修改菜单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdateCatalog(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CatalogForm from=(CatalogForm)form;
		Integer id=Integer.valueOf(request.getParameter("c_id"));
		from.setCatalog(catalogService.getDatailCatalog(id));
		return mapping.findForward("toUpdateCatalog");
	}
	
	
	/**
	 * 排序
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward orderCataglog(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CatalogForm from=(CatalogForm)form;
		 
		return super.goSuccess("保存顺序成功", "/catalog.do?method=toShowCatalog&p_id="+from.getCatalog().getParentId(), request, response, mapping);
	}
	
	
	
	
}
